Login = function(){
    // 登录Panel
    var win;
    var formPanel;
    
    // 使登录窗口居中显示
    window.onresize = function(){
        var x = (document.body.clientWidth - win.width) / 2;
        var y = (document.body.clientHeight - win.height) / 2;
        
        var windiv = document.getElementById("loginDialogDiv");
        
        windiv.style.position = "absolute";
        windiv.style.left = x;
        windiv.style.top = y+40;
    };
    
    return {
        Init: function(){
            Ext.QuickTips.init();
            
            /**
             * 表单Panel
             */
            formPanel = new Ext.form.FormPanel({
                region: 'center',
                baseCls: 'x-plain',
                defaults: {
                    width: 185,
                    height: 24
                },
                labelAlign: 'right',
                frame: false,
                height: 70,
                id: 'loginForm',
                items: [{
                    xtype: 'textfield',
                    labelSeparator: '',
                    fieldLabel: '帐号',
                    hideLabel: false,
                    id: 'username',
                    name: 'username',
                    allowBlank: false,
					blankText : '帐号不能为空'
                }, {
                    xtype: 'textfield',
                    labelSeparator: '',
                    fieldLabel: '密码',
                    hideLabel: false,
                    inputType: 'password',
                    id: 'password',
                    name: 'password',
                    allowBlank: false,
					blankText : '密码不能为空'
                }, {
                    xtype: 'textfield',
                    labelSeparator: '',
                    fieldLabel: '验证码',
                    hideLabel: false,
                    id: 'randCode',
                    name: 'randCode',
                    width: 80,
                    allowBlank: false,
					blankText : '验证码不能为空'
                }]
            });
            
            /**
             * 登录框Panel
             */
            win = new Ext.Panel({
                draggable: false,
                bodyStyle: "background:url(images/denglujiemian.png) no-repeat;",
                width: 745,
                height: 409,
                border: false,
                footer: false,
                bodyBorder: false,
                id: 'login-win',
                renderTo: 'loginDialogDiv',
                layout: 'border',
                items: [formPanel, {
                    xtype: 'button',
                    text: '提交',
                    id: 'loginBtn',
                    width: 78,
                    height: 19
                
                }, {
                    xtype: 'button',
                    text: '重置',
                    id: 'resetBtn',
                    width: 78,
                    height: 19,
					handler:function(){
						formPanel.form.reset();
					}
                }],
                keys: {
                    key: 13,
                    fn: Login.dologin
                }
            });
            
            // 显示登录框
            win.show();
            
            // 设置表单绝对位置
            formPanel.setPosition(350, 70);
            
            // 设置登录按钮绝对位置
            Ext.getCmp("loginBtn").setPosition(465, 160);
            Ext.getCmp("resetBtn").setPosition(560, 160);
            
            var loginbtn = document.getElementById("loginBtn");
            
            var bd = Ext.getDom('randCode');
            var bdParent = Ext.get(bd.parentNode);
            bdParent.createChild([{
                tag: 'span',
                html: '<a href="javascript:reloadcode();" >'
            }, {
                tag: 'img',
                id:'validateCode',
                src: 'validateCodePage.jsp',
                align: 'absbottom'
            },{
            	tag: 'span',
            	html:'</a>'
            }]);
            
            loginBtn.onclick = function(){
                Login.dologin();
            }
        },
        
        dologin: function(){
            var username = Ext.getCmp("username").getValue();
            var password = Ext.getCmp("password").getValue();
            var randCode = Ext.getCmp("randCode").getValue();
            
            if (username == "" || username == null) {
				Ext.getCmp("username").markInvalid("请输入用户名。");
                return;
            }
            
            if (password == "" || password == null) {
				Ext.getCmp("password").markInvalid("请输入密码。");
                return;
            }
            
            if (randCode == "" || randCode == null) {
				Ext.getCmp("randCode").markInvalid("请输入验证码。");
                return;
            }
            
            // 验证合法后使用加载进度条   
            Ext.MessageBox.show({
                title: '请稍等',
                msg: '正在加载...',
                progressText: '',
                width: 200,
                progress: true,
                closable: false,
                animEl: 'loding'
            });
            
            // 控制进度速度   
            var f = function(v){
                return function(){
                    var i = v / 11;
                    Ext.MessageBox.updateProgress(i, '');
                };
            };
            
            for (var i = 1; i < 13; i++) {
                setTimeout(f(i), i * 150);
            }
            
            // 提交到服务器操作   
            formPanel.form.doAction('submit', {
                url: 'loginServlet',
                method: 'post',
                
                success: function(form, action){
                    document.location='index.jsp';   
                },
                failure: function(form, action){
					if (action.failureType == Ext.form.Action.SERVER_INVALID) {
						if(action.result.errors=='userOrPassword')
						{
							Ext.MessageBox.hide();
							Ext.getCmp("username").markInvalid("用户名或密码错误。");
							Ext.getCmp("password").markInvalid("用户名或密码错误。");
						}
						
						if(action.result.errors=='validateCode')
						{
							Ext.MessageBox.hide();
							Ext.getCmp("randCode").markInvalid("验证码错误。");
						}
						
						document.getElementById("validateCode").src = "validateCodePage.jsp?" + new Date().getTime();
					}
                }
            });
        }
    };
}();

Ext.onReady(Login.Init, Login, true);

function reloadcode(obj) {
	document.getElementById("validateCode").src = "validateCodePage.jsp?" + new Date().getTime();
}
